<?php

namespace App\Http\Controllers;

use App\FormModel;
use Illuminate\Http\Request;

class WebController extends Controller
{
    public function form(Request $request){
        $id = $request->input('id');
        $uqur = FormModel::where('id',$id)->first();
        $data = ['uqur'=>$uqur];
        return view("form")->with($data);
    }

    public function add(Request $request)
    {
        $input = $request->input();
        $id = $input['id'];
        unset($input['_token']);
        if($id==0){
            $img = $request->file('pic');
            $img->move(public_path('uploads'),$img->getClientOriginalName());
            $input['img'] = "uploads/".$img->getClientOriginalName();
            $ok = FormModel::create($input);
            if($ok){
                return "OK";
            }else{
                return "NO";
            }
        }else{
            $mazmun = FormModel::where('id',$id)->first();
            $mazmun->update($input);
        }

    }

    public function delete(Request $request)
    {
        $id = $request->input("id");
        FormModel::where('id',$id)->delete();
    }

    public function table()
    {
        $list = FormModel::get();
        $data = ['list'=>$list];
        return view("table")->with($data);

        //第二种
//        return view("table",compact("list"));
//        return view('table')->with(compact("list"));

    }

    public function test(){
        $arr = ["name"=>"Abduhelim","age"=>26];
        $a = compact("arr");
//        $a = ["arr"=>$arr];
        dd($a);
    }

}
